package com.card.advice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.card.main.CardOnline;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.db.FilterOperator;
import com.xiaoao.net.db.Query;
import com.xiaoao.net.db.Query.FilterPredicate;
import com.xiaoao.net.utils.EntityUtils;
import com.xiaoao.net.utils.json.GSONUtils;

/**
 * 反馈系统统一管理类
 * @author hiChina
 *
 */
public class AdviceManager {
	private static final AdviceManager instance = new AdviceManager();
	public static AdviceManager getInstance() {
		return instance;
	}
	
	public static final String T_ADVICE_DATA = "Advicedata";
	
	public AdviceManager(){
		init();
	}
	//反馈信息
	private List<Advice> adviceList = new ArrayList<>();
	//上面list的map映射
	private Map<String, Advice> adviceMap = new ConcurrentHashMap<>();
	
	/**
	 *游戏中添加反馈消息
	 */
	public void sendNewAdvice(Advice advice){
		//添加反馈意见到反馈系统列表
		adviceList.add(advice);
		adviceMap.put(advice.getAdviceId(), advice);
		//添加数据库
		advice.saveDb();
	}
	
	public void init(){
		List<Advice> dbList = getAdviceFromDB();
		this.adviceList.clear();
		this.adviceMap.clear();
		for (Advice advice : dbList) {
			this.adviceList.add(advice);
			this.adviceMap.put(advice.getAdviceId(),advice);
		}
	}
	
	/**
	 * 从数据库获取反馈信息列表
	 * @return
	 */
	public List<Advice> getAdviceFromDB() {
		List<Advice> dbList = new ArrayList<>();
		//从数据库获取玩家数据
		Query query = new Query(T_ADVICE_DATA);
		query.addFilter(new FilterPredicate(Advice.STATE, FilterOperator.EQUAL, CardOnline.ADVICE_STATE_VALID));
		query.setLimit(10000);//限制查询数量
		List<Entity> adviceEntityList = DBManager.getInstance().queryEntity(query);
		
		for(Entity entity:adviceEntityList){
			Advice advice = new Advice(entity);
			dbList.add(advice);
		}
		return dbList;
	}
	
	/**
	 * 根据id获取反馈意见内容
	 * @param id
	 * @return
	 */
	public Advice getAdviceMap(String id){
		return adviceMap.get(id);
	}
	
	/**
	 * hiChina:unit test
	 */
	public void testAddToDB(){
		Advice advice = new Advice("!!!zhc_test!!!","游戏很不错！",new Date(),CardOnline.ADVICE_STATE_VALID);
		sendNewAdvice(advice);
	}
	
	/**
	 * 获取反馈信息的json数据
	 * @return json类型
	 */
	public String getAdvicesJson(){
		Gson gson = new GsonBuilder()
				.setDateFormat(GSONUtils.DATEFORMAT)
				.disableHtmlEscaping()
				.create();
		return gson.toJson(getAdviceFromDB());
	}
	
	/**
	 * 更新反馈信息状态
	 * @param id
	 * @return
	 */
	public boolean deleteAdvice(String id) {
		if(id == null || id.isEmpty()) {
			return false;
		}
		Advice advice = adviceMap.get(id);
		if(advice != null) {
			Entity entity = new Entity(AdviceManager.T_ADVICE_DATA,id);
			entity.setProperty(Advice.STATE, CardOnline.ADVICE_STATE_INVALID);
			EntityUtils.Store(entity);
			return true;
		}
		return false;
	}
	
	/**
	 * hiChina:unit test
	 */
	public static void main(String[] args) {
		AdviceManager.getInstance().testAddToDB();
		List<Advice> adviceList = AdviceManager.getInstance().getAdviceFromDB();
		for (Advice advice : adviceList) {
			System.out.println(advice.toString());
		}
	}
}
